﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using IndexMarkSilverlightClient.Classes;
using IndexMarkSilverlightClient.IndexMarkServiceReference;
using System.IO;
using System.Text;

namespace IndexMarkSilverlightClient.Views.Controls.ReportsControls
{
    public partial class Nadbavki : UserControl
    {
        IndexMarkServiceReference.IndexMarkServiceClient client = new IndexMarkServiceClient();

        private ObservableCollection<Money> money = new ObservableCollection<Money>();

        private int _IDTerm=8;


        private bool GroupByFaculty = false;
        private bool GroupByKafedra = false;
        private bool GroupByPosition = false;
        //private IndexStrategyType StrategyType;

        public PagedCollectionView GroupedMoney
        {
            get
            {
                
                PagedCollectionView ind = new PagedCollectionView(new List<Money>(this.money));
                ind.GroupDescriptions.Clear();
                if (GroupByFaculty)
                {
                    PropertyGroupDescription pg = new PropertyGroupDescription("Resp.Department.Parent.Abbriviation");
                    ind.GroupDescriptions.Add(pg);
                }
                if (GroupByKafedra)
                {
                    PropertyGroupDescription pg = new PropertyGroupDescription("Resp.Department.Abbriviation");
                    ind.GroupDescriptions.Add(pg);
                }
                if (GroupByPosition)
                {
                    PropertyGroupDescription pg = new PropertyGroupDescription("Resp.Position");
                    ind.GroupDescriptions.Add(pg);
                }


                return ind;
            }
        }


        public Nadbavki(IndexStrategyType strategy = IndexStrategyType.PPS)
        {
            //StrategyType = strategy;
            InitializeComponent();
            Loaded += new RoutedEventHandler(NadbavkiLoaded);
        }

        void NadbavkiLoaded(object sender, RoutedEventArgs e)
        {
            var vm = MainPage.ViewModel;
            vm.SelectTerm -= ViewModel_SelectTerm;
            vm.SelectTerm += new MainViewModel.SelectTermEventHandler(ViewModel_SelectTerm);
            vm.SelectTermInit();

            client.GetPremiumsCompleted += GetMoneycomplite;
            client.GetPremiumsAsync(_IDTerm);
        }

        void GetMoneycomplite(object sender, GetPremiumsCompletedEventArgs e)
        {
            money = e.Result;
            int i = 0;
            foreach (Money m in money)
            {
                m.OrderField = i++;
            }
            IndexGrid.DataContext = GroupedMoney;
        }

        void ViewModel_SelectTerm(object sender, SelectTermEventsArgs e)
        {
            _IDTerm = e.SelectTerm.IDTerm;

        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            ((PagedCollectionView) IndexGrid.ItemsSource).Filter = (obj) =>
            {
                return
                    String.IsNullOrEmpty(
                        (sender as TextBox).Text) ||
                        (obj as Money).Resp.Person.
                            LastName.ToLower().Contains(
                                (sender as TextBox).Text.
                                    ToLower());
            };
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            GroupByFaculty = (sender as CheckBox).IsChecked.Value;
            IndexGrid.DataContext = GroupedMoney;
        }

        private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
        {
            GroupByKafedra = (sender as CheckBox).IsChecked.Value;
            IndexGrid.DataContext = GroupedMoney;
        }

        private void CheckBox_Checked_2(object sender, RoutedEventArgs e)
        {
            GroupByPosition = (sender as CheckBox).IsChecked.Value;
            IndexGrid.DataContext = GroupedMoney;
        }

        private void IndexGrid_LoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e)
        {
            e.RowGroupHeader.PropertyNameVisibility = Visibility.Collapsed;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ExportToExel(money);
        }

        void ExportToExel(ObservableCollection<Money> indexRows)
        {

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Простой файл электронных таблиц | *.csv";
            if (sfd.ShowDialog().Value)
                using (StreamWriter SW = new StreamWriter(sfd.OpenFile(), Encoding.Unicode))
                {
                    SW.Write("№;");
                    SW.Write("ФИО;");
                    SW.Write("Кафедра;");
                    SW.Write("Должность;");
                    SW.Write("Надбавка;");
                    SW.WriteLine();

                    foreach (var row in indexRows)
                    {
                        SW.Write(row.OrderField + ";");
                        SW.Write(row.Resp.Person + ";");
                        SW.Write(row.Resp.Department.Abbriviation + ";");
                        SW.Write(row.Resp.Position + ";");
                        SW.WriteLine(row.Premium + ";");
                    }

                    SW.Close();
                }
        }
    }
}
